package Servlet;

import bean.User;
import dao.UserDao;
import net.sf.json.JSONArray;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

@WebServlet(name = "SearchPeople", value = "/SearchPeople")
public class SearchPeople extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        String id = request.getParameter("id");
        String type = request.getParameter("type");
        PrintWriter out = response.getWriter();
        if(type.equals("0")){
            UserDao userDao = new UserDao();
            List<String> users = userDao.findAllFriend(id);
            List<User> user = new ArrayList<>();
            for(int i = 0 ; i<users.size() ; i++){
                user.add(userDao.findUser(users.get(i)));
            }
            out.print(JSONArray.fromObject(user));
        }else{
            UserDao userDao = new UserDao();
            User user = userDao.findUser(id);

            out.print(JSONArray.fromObject(user));
        }


    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
